package com.bum.api.core.service.ext;

import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.bum.api.core.exp.IApiService;
import com.bum.api.core.exp.om.Metas;
import com.bum.api.core.exp.om.Params;
import com.bum.api.core.service.process.IApiProcess;

public class OlaApiExtService implements IApiService {
	/**
	 * JSON PROCESSORS
	 */
	Map<String, IApiProcess>	jsonProcessors;

	public void setJsonProcessors(Map<String, IApiProcess> jsonProcessors) {
		this.jsonProcessors = jsonProcessors;
	}

	@Override
	public String getJson(String path, Params params, Metas meta) {
		if (StringUtils.isBlank(path)) {
			return null;
		}

		String[] parts = StringUtils.split(path, "/", 2);

		IApiProcess apiProcess = jsonProcessors.get(parts[0]);
		if (apiProcess == null) {
			return "Invalid Service";
		}

		return apiProcess.process(path, params, meta);
	}

	/**
	 * HTML PROCESSORS
	 */
	Map<String, IApiProcess>	htmlProcessors;

	public void setHtmlProcessors(Map<String, IApiProcess> htmlProcessors) {
		this.htmlProcessors = htmlProcessors;
	}

	@Override
	public String getHtml(String path, Params params, Metas metas) {
		if (StringUtils.isBlank(path)) {
			return null;
		}

		String[] parts = StringUtils.split(path, "/", 2);

		IApiProcess apiProcess = htmlProcessors.get(parts[0]);
		if (apiProcess == null) {
			return "Invalid Service";
		}

		String html = apiProcess.process(path, params, metas);
		if (StringUtils.isNotBlank(html)) {
			html = html.replaceAll("\\s+", " ").trim();
		}
		return html;
	}

	/**
	 * HTML PROCESSORS
	 */
	Map<String, IApiProcess>	pageProcessors;

	public void setPageProcessors(Map<String, IApiProcess> pageProcessors) {
		this.pageProcessors = pageProcessors;
	}

	@Override
	public String getPage(String path, Params params, Metas meta) {
		return null;
	}

}
